/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.IO;
using SilverChart.Core.Log;
using SilverChart.Core.Service;
using SilverChart.Core.Configuration;
using System.Windows.Media;

namespace SilverChart.Configuration {
    /// <summary>
    /// Description of Configurations.
    /// </summary>
    [ServiceProvider]
    [ConfigurationItem(Configurations.ROOT_ITEM_NAME, IsRootItem = true)]
    public class Configurations : AbstractConfigurationItem {
        public const String ROOT_ITEM_NAME = "SilverChart";

        public const String ITEM_NAME_TITLE = "title";
        public const String DefaultTitle = "";
        private ValueItemHelper<String> title;

        public const String ITEM_NAME_TITLE_COLOR = "title_color";
        public readonly static Color DefaultTitleColor = Color.FromArgb(0xFF, 0x4B, 0x00, 0x82);//#FF4B0082
        private ValueItemHelper<Brush> titleColor;

        public const String ITEM_NAME_BACKGROUNDCOLOR = "background";
        public readonly static Color DefaultBackgroundColor = Color.FromArgb(0xFF, 0xF8, 0xF8, 0xD8); //"#FFF8F8D8"
        private BrushValueItemHelper backgroundColor;

        private static Configurations _instance;

        public static Configurations CreateInstanceFromString(String configString) {
            _instance = (Configurations)ServiceManager.Instance.GetService<ConfigurationService>().
                ParseConfiguration(configString, ROOT_ITEM_NAME);
            return _instance;
        }

        public static Configurations Instance {
            get {
                return _instance;
            }
        }

        public Configurations(ConfigurationParser parser)
            : base(parser) {
            title = new StringValueItemHelper(this, ITEM_NAME_TITLE, DefaultTitle);
            titleColor = new BrushValueItemHelper(this, ITEM_NAME_TITLE_COLOR, DefaultTitleColor);

            backgroundColor = new BrushValueItemHelper(this, ITEM_NAME_BACKGROUNDCOLOR, DefaultBackgroundColor);
            //Loggers = new JsonLoggerConfigurations();
        }

        public String Title { get { return title.Value; } }
        public Brush TitleColor { get { return titleColor.Value; } }

        private IList<Element> elements = null;
        public const String ITEM_NAME_ELEMENTS = "elements";
        public const String ITEM_NAME_ELEMENT = "element";
        public IList<Element> Elements {
            get {
                if (elements == null) {
                    ConfigurationItem elementsItem;
                    if (!TryGetConfigurationValue<ConfigurationItem>(ITEM_NAME_ELEMENTS, out elementsItem) ||
                        !elementsItem.TryGetConfigurationValues<Element>(ITEM_NAME_ELEMENT, out elements)) {
                        elements = new List<Element>();
                    }
                }
                return elements;
            }
        }

        private XAxis xAxis = null;
        public const String ITEM_NAME_XAXIS = "x_axis";
        public XAxis XAxis {
            get {
                if (xAxis == null) {
                    xAxis = GetConfigurationValue<XAxis>(ITEM_NAME_XAXIS);
                }
                return xAxis;
            }
        }

        private YAxis yAxis = null;
        public const String ITEM_NAME_YAXIS = "y_axis";
        public YAxis YAxis {
            get {
                if (yAxis == null) {
                    yAxis = GetConfigurationValue<YAxis>(ITEM_NAME_YAXIS);
                }
                return yAxis;
            }
        }

        public Brush BackgroundColor { get { return backgroundColor.Value; } }

        private Legend legend = null;
        public const String ITEM_NAME_LEGEND = "legend";
        public Legend Legend {
            get {
                if (legend == null) {
                    legend = GetConfigurationValue<Legend>(ITEM_NAME_LEGEND);
                }
                return legend;
            }
        }

        //[JsonProperty("logs")]
        //public JsonLoggerConfigurations Loggers { get; set; }
    }
}
